package com.gylang.print.richtext.processor;

import com.gylang.print.richtext.Coords;
import com.gylang.print.richtext.pdf.RichPdfRender;
import com.gylang.print.richtext.resolver.StyleResolver;
import com.gylang.print.richtext.component.*;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.List;

/**
 * @author gylang
 * data 2021/7/14
 */
public class HrProcessor implements StyleResolver, RichPdfRender<BaseRichHr> {

	@Override
	public Coords render(Coords lastNodeCoords, RichBox box, BaseRichHr richHr) {

		// 分割线
		PDPageContentStream contentStream = box.getContentStream();
		try {
			contentStream.addRect(box.getMarginL(), lastNodeCoords.getY() - box.getHeightLine(), box.getWidth(), 1);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return Coords.of(box.getMarginL(), lastNodeCoords.getY() - 2 * box.getHeightLine()* box.getFontSize());


	}

	@Override
	public boolean resolve(Element element, List<BaseRich> component, BaseRich parent) {
		if ("hr".equalsIgnoreCase(element.tagName())) {
			BaseRichHr richHr = new BaseRichHr();
			richHr.copyStyle(parent);
			component.add(richHr);
			return true;
		}
		return false;
	}
}
